<template>
	<z-container title="创建订单">
		<uv-gap height="28rpx"></uv-gap>
		<view class="bg-white ss-p-x-32 ss-p-t-24 ss-flex-col gap-12">
			<InputItem label="车牌号" type="cart" v-model="form.car_number" />
			<InputItem label="客户姓名" v-model="form.c_name" />
			<InputItem label="客户电话" maxlength="11" inputType="number" v-model="form.c_mobile" />
			<InputItem label="行驶里程" inputType="digit" v-model="form.licheng" />
			<InputItem label="品牌" type="select" placeholder="请选择" v-model="form.brand" />
			<InputItem label="车型"  v-model="form.carxing" />
			<InputItem label="业务类型" type="select" selectType="yewulists" v-model="form.business_id" placeholder="请选择" />
			<InputItem label="检查报告" type="lookReport" v-model="form.jiance_id" className="ss-m-y-12" />
			<InputItem label="维修方案" type="repairPlan" :required="false" className="ss-m-t-12" v-model="form.wx_json" />
		</view>
		<uv-gap height="50"></uv-gap>
		<z-bottom>
			<view class="ss-p-x-32 ss-p-y-16">
				<z-button @tap="changeTips">发送订单</z-button>
			</view>
		</z-bottom>
		<z-modaltips @confirm="confirmSend" v-model="state.tipsShow" :content="state.tips"></z-modaltips>
	</z-container>
</template>

<script setup>
	import sheep from "@/sheep"
	import InputItem from "@/pages/components/InputItem.vue"
	const state = reactive({
		tipsShow: false,
		tips: '订单发送给前台审核后将无法修该，确认发送吗？'
	})
	const form = reactive({
		car_number: '', //车牌号
		c_name: '', //客户姓名
		c_mobile: '', //手机号
		licheng: '', //里程
		brand: '', //品牌id
		carxing:'', //车型
		business_name: '', //业务类型名称
		business_id: '', //业务类型id
		jiance_id: '', //检测报告id
		wx_json: '' //维修json
	})
	onMounted(() => {
		uni.$on('get_jiance_id', res => {
			form.jiance_id = res
		})
	})
	onUnmounted(() => {
		uni.$off('get_jiance_id')
	})

	function changeTips() {
		if (!form.car_number) {
			uni.showToast({
				title: '请输入车牌号',
				icon: 'none'
			})
			return
		}
		if (!form.c_name) {
			uni.showToast({
				title: '请输入客户姓名',
				icon: 'none'
			})
			return
		}
		if (!form.c_mobile) {
			uni.showToast({
				title: '请输入客户电话',
				icon: 'none'
			})
			return
		}
		if (!form.licheng) {
			uni.showToast({
				title: '请输入行驶里程',
				icon: 'none',
			})
			return
		}
		if (!form.brand) {
			uni.showToast({
				title: '请选择品牌',
				icon: 'none'
			})
			return
		}
		if (!form.carxing) {
			uni.showToast({
				title: '请输入车型',
				icon: 'none'
			})
			return
		}
		if (!form.business_id) {
			uni.showToast({
				title: '请选择业务类型',
				icon: 'none'
			})
			return
		}
		if (!form.jiance_id) {
			uni.showToast({
				title: '请选择检查报告',
				icon: 'none'
			})
			return
		}
		// if (!form.wx_json) {
		// 	uni.showToast({
		// 		title: '请选择维修方案',
		// 		icon: 'none'
		// 	})
		// 	return
		// }
		state.tipsShow = !state.tipsShow
	}

	async function confirmSend() {
		let formData = JSON.parse(JSON.stringify(form))
		if(form.wx_json) {
			let wx_json = JSON.parse(JSON.parse(JSON.stringify(form.wx_json)))
			// 转换逻辑：分别把 remake 添加进 peijian 和 tiaojiao 对象中
			const result = wx_json.map(item => {
			  const peijian = (item.peijian || []).map(p => ({ ...p, remake: item.remake }));
			  const tiaojiao = (item.tiaojiao || []).map(t => ({ ...t, remake: item.remake }));
			  return [...peijian, ...tiaojiao];
			});
			formData.wx_json = JSON.stringify(result)
		}
		const { code,data } = await sheep.$api.user.createorder(formData)
		if(code != 1) return
		setTimeout(() => sheep.$router.back(),1000)
		// sheep.$router.redirect('/pages/order/confirmOrder')
	}
</script>

<style>

</style>